package com.track.mapping.content;

import java.io.File;
import java.io.InputStream;

import com.track.mapview;

import android.os.Environment;
import android.util.Log;

public class SDcard {

	private static SDcard _SD;
	
	protected void onCreate()
	{
		// SIGNLETON
		if(_SD != null) return;
		_SD = this;
		
	}
	
	public static SDcard getInstance()
	{
		return _SD;
	}
	
	
	public String getSlippyTileStream(int x, int y ){
		
		double xL = mapview.currentMapLat;
		double yL = mapview.currentMapLon;
		int z		= mapview.currentZoomLevel;
		
		/*
		String path =  Environment.getExternalStorageDirectory().getPath() + 
			"/bsb_root/rnc/"+z+"/"+ 
			((int)getTileXvalue(yL,z)+x)+
			"/"+((int)getTileYvalue(xL, z)+y)+
			".png";
		*/
		
		/*
		String path =  Environment.getExternalStorageDirectory().getPath() + 
		"/bsb_root/rnc/"+getTileNumber(yL, xL, z)+
		".png";
		*/
		
		String path =  Environment.getExternalStorageDirectory().getPath() + 
		"/bsb_root/rnc/11/"+(603+x)+"/"+(1278+y)+".png";
		Log.w("SDCard path - ", path);
		
		return path;
	}
	
	/*
	protected String getGoogleTilePath()
	{
		return ("http://mt0.google.com/mt/v=w2.92&hl=en&x=" + getTileXvalue(mapview.currentMapLon, mapview.currentZoomLevel) + "&y=" + getTileYvalue(mapview.currentMapLat, mapview.currentZoomLevel)+ "&z=" + mapview.currentZoomLevel + "&s=Gali");
	}
	*/
	/* this IS Athe sample code
	 public class slippytest {
 public static void main(String[] args) {
   int zoom = 10;
   double lat = 47.968056d;
   double lon = 7.909167d;
   System.out.println("http://tile.openstreetmap.org/" + getTileNumber(lat, lon, zoom) + ".png");
 }
 public static String getTileNumber(final double lat, final double lon, final int zoom) {
   int xtile = (int)Math.floor( (lon + 180) / 360 * (1<<zoom) ) ;
   int ytile = (int)Math.floor( (1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * (1<<zoom) ) ;
    return("" + zoom + "/" + xtile + "/" + ytile);
   }
 }
	*/
	
	 public String getTileNumber(final double lat, final double lon, final int zoom) {
		   int xtile = (int)Math.floor( (lon + 180) / 360 * (1<<zoom) ) ;
		   int ytile = (int)Math.floor( (1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * (1<<zoom) ) ;
		    return("" + zoom + "/" + xtile + "/" + ytile);
		   }
	
	 private int getTileXvalue(final double lon, final int zoom){
		 
		 
		 return ((int)Math.floor( (lon + 180) / 360 * (1<<zoom) ));
	 }
	 
	 private int getTileYvalue(final double lat, final int zoom){
		 
		 return ((int)Math.floor( (1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * (1<<zoom)));
	 }
}
